package com.jp.knowledge.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jp.knowledge.R;
import com.jp.knowledge.util.PackInfo;
import com.jp.knowledge.view.DotLayout;
import com.jp.knowledge.view.JpApplicationWebView;

import java.util.ArrayList;
import java.util.List;

import static android.content.Context.MODE_PRIVATE;

/**
 * Created by 528787067@qq.com on 2017/3/6.
 *
 * 欢迎页面对话框
 */

public class WelDialog extends Dialog implements View.OnClickListener {

    private static final long DIALOG_SHOW_TIME = 4000;

    private Context mContext;
    private View contentView;
    private View welcomeView;
    private RelativeLayout guideView;
    private ViewPager guidePages;
    private TextView jumpOverBtn;
    private TextView enterBtn;
    private DotLayout dotLayout;
    private int[] guideResources;
    private List<View> pageViews;

    private SharedPreferences sharedPreferences;
    private int historyVersionCode;
    private int currentVersionCode;

    public WelDialog(Context context) {
        super(context, R.style.welcome_dialog);
        guideResources = new int[]{
                /*R.mipmap.welcome_guide_1,*/
                R.mipmap.welcome_guide_2,
                R.mipmap.welcome_guide_3,
                R.mipmap.welcome_guide_4,
                R.mipmap.welcome_guide_5};
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setWindowAnimations(R.style.welcome_dialog_anim);
        setCanceledOnTouchOutside(false);
        setCancelable(false);

        mContext = getContext();
        contentView = getLayoutInflater().inflate(R.layout.activity_welcom, null);
        welcomeView = contentView.findViewById(R.id.welcome_view);
        guideView = (RelativeLayout) contentView.findViewById(R.id.guide_view);
        guidePages = (ViewPager) contentView.findViewById(R.id.guide_pages);
        jumpOverBtn = (TextView) contentView.findViewById(R.id.guide_jump_over);
        enterBtn = (TextView) contentView.findViewById(R.id.guide_enter);
        dotLayout = (DotLayout) contentView.findViewById(R.id.guide_doc);

//        initData();
        initGuideView();
//        initApplicationWebView();
        setContentView(contentView);
    }



    private void initData(){
        sharedPreferences = mContext.getSharedPreferences("PackInfo", MODE_PRIVATE);
        historyVersionCode = sharedPreferences.getInt("VersionCode", -1);
        currentVersionCode = PackInfo.getVersionCode(mContext);
    }

    private void initGuideView(){
//        if(historyVersionCode == currentVersionCode){
        if(true){
            guideView.setVisibility(View.GONE);
            return;
        }
        guideView.setVisibility(View.VISIBLE);
        jumpOverBtn.setOnClickListener(this);
        enterBtn.setOnClickListener(this);
        pageViews = new ArrayList<>();
        for(int resourceId : guideResources){
            ImageView imageView = new ImageView(mContext);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setImageResource(resourceId);
            pageViews.add(imageView);
        }
        dotLayout.initView(pageViews.size());
        guidePages.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return pageViews.size();
            }
            @Override
            public boolean isViewFromObject(View view, Object object) {
                return (view == object);
            }
            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView(pageViews.get(position));
            }
            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                container.addView(pageViews.get(position));
                return pageViews.get(position);
            }
        });
        dotLayout.getChildAt(guidePages.getCurrentItem()).setBackgroundResource(R.drawable.dot_select);
        guidePages.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            private int lastPosition = guidePages.getCurrentItem();
            @Override
            public void onPageScrollStateChanged(int state) {}
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
            @Override
            public void onPageSelected(int position) {
                dotLayout.getChildAt(lastPosition).setBackgroundResource(R.drawable.dot_com);
                dotLayout.getChildAt(position).setBackgroundResource(R.drawable.dot_select);
                if(position == pageViews.size()-1){
                    jumpOverBtn.setVisibility(View.GONE);
                    enterBtn.setVisibility(View.VISIBLE);
                } else{
                    jumpOverBtn.setVisibility(View.VISIBLE);
                    enterBtn.setVisibility(View.GONE);
                }
                lastPosition = position;
            }
        });
    }

    private void initApplicationWebView(){
//        FrameLayout backgroundView = (FrameLayout) contentView.findViewById(R.id.backgroundView);
//        JpApplicationWebView webView = JpApplicationWebView.getInstance(mContext);
//        if(webView.getParent() != null)
//            ((ViewGroup)webView.getParent()).removeView(webView);
//        backgroundView.addView(webView);
    }

    @Override
    public void show() {
        super.show();
        contentView.postDelayed(new Runnable() {
            @Override
            public void run() {
                if(guideView.getVisibility() == View.GONE) {
                    dismiss();
                } else{
                    Animation hideAnima = AnimationUtils.loadAnimation(mContext, R.anim.welcome_dialog_anim_exit);
                    welcomeView.startAnimation(hideAnima);
                    welcomeView.setVisibility(View.GONE);
                }
            }
        }, DIALOG_SHOW_TIME);
    }

    @Override
    public void onClick(View v) {
        if(v == jumpOverBtn || v == enterBtn) {
            sharedPreferences.edit().putInt("VersionCode", currentVersionCode).apply();
            dismiss();
        }
    }
}
